package com.logos.musicframe.listener;

import com.google.common.base.Preconditions;
import com.logos.musicframe.constant.MqConstant;
import com.logos.musicframe.entity.LikedCount;
import com.logos.musicframe.entity.SongsLiked;
import com.logos.musicframe.mapper.LikedCountMapper;
import com.logos.musicframe.mapper.SongsLikedMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author logos
 * date 2025/6/19 10:19
 * @version v1.0
 * @Package com.logos.musicframe.listener
 */
@Component
@Slf4j
public class SongsListener {

    @Autowired
    private SongsLikedMapper songsLikedMapper;

    @Autowired
    private LikedCountMapper likedCountMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.SONGS_LIKED_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstant.SONGS_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {MqConstant.SONGS_LIKED_ROUTING_KEY}
    ))
    public void listenLikeQueue(SongsLiked songsLiked){
        log.info("收到点赞消息：{}", songsLiked);
        Preconditions.checkNotNull(songsLiked.getSongId(),"点赞消息的歌曲id不能为空");
        Preconditions.checkNotNull(songsLiked.getUserId(),"点赞消息的用户id不能为空");
        songsLiked.setCreateTime(new Date());
        songsLikedMapper.insert(songsLiked);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.SONGS_UNLIKED_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstant.SONGS_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {MqConstant.SONGS_UNLIKED_ROUTING_KEY}
    ))
    public void listenUnLikeQueue(SongsLiked songsLiked){
        log.info("收到取消点赞消息：{}", songsLiked);
        Preconditions.checkNotNull(songsLiked.getSongId(),"取消点赞消息的歌曲id不能为空");
        Preconditions.checkNotNull(songsLiked.getUserId(),"取消点赞消息的用户id不能为空");
        songsLikedMapper.deleteByUserIdAndSongId(songsLiked.getUserId(), songsLiked.getSongId());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.SONGS_LIKED_COUNT_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstant.SONGS_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {MqConstant.SONGS_LIKED_COUNT_ROUTING_KEY}
    ))
    public void listenLikeCountQueue(LikedCount likedCount){
        log.info("收到修改点赞个数消息:{}", likedCount);
        Preconditions.checkNotNull(likedCount.getSongId(),"修改点赞个数消息的歌曲id不能为空");
        Preconditions.checkNotNull(likedCount.getCount(),"修改点赞个数消息的点赞个数不能为空");
        // 现查询是否有这首歌的点赞记录
        LikedCount dbLikeCount = likedCountMapper.queryBySongId(likedCount.getSongId());
        if(dbLikeCount == null){
            // 新增
            likedCountMapper.insert(likedCount);
        }else{
            // 更新
            int count = dbLikeCount.getCount() + likedCount.getCount();
            dbLikeCount.setCount(count);
            likedCountMapper.update(dbLikeCount);
        }
    }
}
